using System;
using System.IO;
using System.Text;
using System.Windows;
using Qplus.beans;
using Qplus.Infrastructure;
using System.Diagnostics;

namespace Qplus.Commands
{

    /**
     *
     *
     */
    public class GetUserCommand : AbsHeaderHttpCommand , HttpInputStreamProcessor{

	    const String COMMAND = "GetUser";
	
	    QUser user;

        public event EventHandler<MessageReceivedFromServer> GetUserCommandReceivedFromHttpServer;

	    /**
	     * @param command
	     * @param uid
	     * @param UKEY
	     */
        public GetUserCommand(String uid, String UKEY)
            : base(COMMAND, uid, UKEY)
        {
		    
	    }
	    //Observer o;
	    /**
	     * @param command
	     * @param uid
	     * @param UKEY
	     */
        public GetUserCommand(long uid, byte[] UKEY, QUser user)
            : base(COMMAND, Convert.ToString(uid)/*uid.GetType().ToString(),*/, Encoding.UTF8.GetString(UKEY, 0, 32))
        {
		    this.user = user;
		   
	    }

	    public HttpTask execute(String url) {
            base.MessageReceivedFromHttpServer += new EventHandler<MessageReceivedFromServer>(GetUserCommand_MessageReceivedFromHttpServer);
            return base.execute(url, this);
        }

        void GetUserCommand_MessageReceivedFromHttpServer(object sender, MessageReceivedFromServer e)
        {
            //throw new NotImplementedException();
            string data = System.Text.UTF8Encoding.UTF8.GetString(e.Message, 0, e.Message.Length);
            Debug.WriteLine(data);
            ProcessIncomingMessage(data);
        }
        private void ProcessIncomingMessage(string ReceivedMessage)
        {
            // Future hook for handling the message in somewhere else.
            // It's most probably wise to put the parsing logic in some other class.
            if (GetUserCommandReceivedFromHttpServer != null)
                GetUserCommandReceivedFromHttpServer(this, new MessageReceivedFromServer(ReceivedMessage));
        }

	    
	    public override Boolean processInputStream(Stream responseStream) {
		    //TODO
            return true;
	    }

	    
	    public override void onSuccess(int code) {
	    }


        public override void onFailed(int code)
        {
	    }
    }
}
